package com.example.koizzi.ad20180516_amap_kfc_3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;
import java.util.regex.Matcher;

public class MainActivity extends AppCompatActivity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener {
    MapView mMapView = null;
    private Button btnSerch;
    private EditText edSerch;
    AMap aMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initMap(savedInstanceState);
        initView();
    }

    private void initView() {
        btnSerch = findViewById(R.id.btn_Serach);
        edSerch = findViewById(R.id.ed_Serch);
        btnSerch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key = edSerch.getText().toString();
                PoiSearch.Query query = new PoiSearch.Query(key, "", "020");
                query.setPageSize(10);
                query.setPageNum(0);

                PoiSearch poiSearch = new PoiSearch(MainActivity.this,query);
                poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(23.18321,113.353111),2000));
                poiSearch.setOnPoiSearchListener(MainActivity.this);
                poiSearch.searchPOIAsyn();
            }
        });
    }

    private void initMap(Bundle savedInstanceState) {
        mMapView = findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        CameraUpdate mc = CameraUpdateFactory.newCameraPosition(
                new CameraPosition(
                        new LatLng(23.18321,113.353111),
                        15,30,0
                )
        );
        aMap.moveCamera(mc);
        aMap.setOnMapLongClickListener(this);
    }


    @Override
    public void onMapLongClick(LatLng latLng) {
        aMap.addMarker(new MarkerOptions().position(latLng).title("广交院").snippet("欢迎光临"));

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        if (poiResult == null) {
            Toast.makeText(MainActivity.this,"当前范围内未搜到",Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<PoiItem> poiList = poiResult.getPois();
        for (PoiItem p : poiList) {
            Log.e("**", p.getTitle());
            double latitude = p.getLatLonPoint().getLatitude();
            double longitude = p.getLatLonPoint().getLongitude();
            LatLng latLng = new LatLng(latitude,longitude);
            aMap.addMarker(new MarkerOptions().position(latLng).title(p.getTitle()).snippet(p.getTel()));
        }

    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }
}
